<?php

require_once dirname(__FILE__).'/../lib/todoGeneratorConfiguration.class.php';
require_once dirname(__FILE__).'/../lib/todoGeneratorHelper.class.php';

/**
 * todo actions.
 *
 * @package    check
 * @subpackage todo
 * @author     Éric Rogé <eric.roge@ui-studio.fr>
 * @version    SVN: $Id: actions.class.php 29 2009-07-27 18:28:06Z Eric.Roge $
 */
class todoActions extends autoTodoActions
{
  public function preExecute()
  {
    $this->configuration = new todoGeneratorConfiguration();

    if (
      'show' !== $this->getActionName()
      && !$this->getUser()->hasCredential($this->configuration->getCredentials($this->getActionName()))
    )
    {
      $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
    }

    $this->dispatcher->notify(new sfEvent($this, 'admin.pre_execute', array('configuration' => $this->configuration)));

    $this->helper = new todoGeneratorHelper();
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->todo = $this->getRoute()->getObject();
    $this->getResponse()->setTitle($this->todo->title);
  }

  public function executePublish(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    $todo = $this->getRoute()->getObject();
    $todo->publish();
    $this->getUser()->setFlash('notice', $todo.' has been published');

    $this->redirect('@todo');
  }

  public function executeBatchPublish(sfWebRequest $request)
  {
    $todos = Doctrine::getTable('Todo')->findByIdList($request->getParameter('ids'));
    foreach ($todos as $todo)
    {
      $todo->publish();
    }
    $this->getUser()->setFlash('notice', 'The selected todos have been published successfully.');

    $this->redirect('@todo');
  }

  public function executeUnpublish(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    $todo = $this->getRoute()->getObject();
    $todo->unpublish();
    $this->getUser()->setFlash('notice', $todo.' has been unpublished');

    $this->redirect('@todo');
  }

  public function executeBatchUnpublish(sfWebRequest $request)
   {
     $todos = Doctrine::getTable('Todo')->findByIdList($request->getParameter('ids'));
     foreach ($todos as $todo)
     {
       $todo->unpublish();
     }
     $this->getUser()->setFlash('notice', 'The selected todos have been unpublished successfully.');

     $this->redirect('@todo');
   }

  public function executePromote(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    $todo = $this->getRoute()->getObject();
    $todo->promote();
    $this->getUser()->setFlash('notice', $todo.' has been moved up in list');

    $this->redirect('@todo');
  }

  public function executeDemote(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    $todo = $this->getRoute()->getObject();
    $todo->demote();
    $this->getUser()->setFlash('notice', $todo.' has been moved down in list');

    $this->redirect('@todo');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($request->hasParameter('_preview'))
    {
      $this->configuration->modePreview = 'on';
      $this->form->updateObject();
      return sfView::SUCCESS;
    }
    if ($form->isValid())
    {
      $notice = $form->getObject()->isNew() ? 'The item was created successfully.' : 'The item was updated successfully.';
      $todo = $form->save();
      $this->dispatcher->notify(new sfEvent($this, 'admin.save_object', array('object' => $todo)));
      if ($request->hasParameter('_save_and_add'))
      {
        $this->getUser()->setFlash('notice', $notice.' You can add another one below.');
        $this->redirect('@todo_new');
      }
      else
      {
        $this->getUser()->setFlash('notice', $notice);
        $this->redirect(array('sf_route' => 'todo_edit', 'sf_subject' => $todo));
      }
    }
    else
    {
      $this->getUser()->setFlash('error', 'The item has not been saved due to some errors.', false);
    }
  }

  /**
   * Fix the request ambiguity on the position columns
   */
  protected function addSortQuery($query)
  {
    $query->addOrderBy($query->getRootAlias().'.position asc');
  }
}
